// 导入joi模块
let joi = require("joi");

// 允许未设置规则的未知键
joi = joi.defaults((schema) =>
  schema.options({
    allowUnknown: true,
  })
);
/**
 * string()值必须是字符串
 * alphanum值只能包括a-zA-Z0-9的字符串
 * min(length)最小长度
 * max(length)最大长度
 * required()值是必填项，不能为undefined
 * pattern(正则表达式)值必须符合正则表达式的规则
 */
// 用户名的校验规则
const username = joi.string().alphanum().min(1).max(10).required();
// 密码的校验规则
const password = joi
  .string()
  .pattern(/^[\S]{6,12}$/)
  .required();
// 验证码
const checkCode = joi.string().alphanum().min(4).max(4).required();
const uuid = joi.number().required();
const nickname = joi.string();
const email = joi.string().email();
const status = joi.number().valid(0, 1);
// 分页
const pageSize = joi.number().required();
const currentPage = joi.number().required();

// 登录表单验证规则对象
exports.user_login_schema = joi.object().keys({
  username,
  password,
  checkCode,
  uuid,
});
// 添加用户表单验证规则对象
exports.add_user_schema = joi.object().keys({
  username,
  password,
  status,
});
// 获取用户列表接口
exports.get_list = joi.object().keys({
  pageSize,
  currentPage,
  status,
});
// 更新用户接口
exports.update_user_schema = joi.object().keys({
  username,
  status,
  nickname,
  email,
});
